<?php

namespace common\models\kaoguobao;

use Yii;
use common\models\BaseModels;
use common\services\tool\GnhString;
use yii\behaviors\TimestampBehavior;

class User extends BaseModels
{
    
    const CRAD_TYPE_ID           = 1;
    const CRAD_TYPE_STUDENT      = 2;
    const CRAD_TYPE_OTHER        = 3;
    const SEX_FEMALE             = 0;
    const SEX_MALE               = 1;
    const INVITE_STATUS_NOT      = 0;
    const INVITE_STATUS_YES      = 1;
    const TYPE_COMMON            = 1;
    const TYPE_VALUE             = 2;
    const TYPE_BUY               = 3;

    //身份证类型
    public static $card_type     = [
        self::CRAD_TYPE_ID       => "身份证",
        self::CRAD_TYPE_STUDENT  => "学生证",
        self::CRAD_TYPE_OTHER    => "其他",
    ];
    //性别
    public static $sex           =[
        self::SEX_FEMALE         => "女",
        self::SEX_MALE           => "男",
    ];
    //是否邀请用户
    public static $invite_status = [
        self::INVITE_STATUS_NOT  => "否",
        self::INVITE_STATUS_YES  => "是",
    ];
    //用户类型
    public static $type          = [
        self::TYPE_COMMON        => "普通用户",
        self::TYPE_VALUE         => "价值用户",
        self::TYPE_BUY           => "购买用户",
    ];
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%user}}';
    }

    /**
     * 切换数据库(测试环境)
     */
    public static function getDb(){
        return Yii::$app->get('db_kgb'); 
    }

    /**
     * 给数据库的处理进行时间的操作
     */
    public function behaviors() {
        return [
            TimestampBehavior::className(),
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [[  'id',
                'is_valid',
                'user_name',
                'wechat_id',
                'phone',
                'card_type',
                'card_number',
                'birthday',
                'sex',
                'mail',
                'invite_status',
                'invite_id',
                'code',
                'key',
                'type',
                'remarks',
                'created_at',
                'updated_at',
            ], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id'            => Yii::t('app_table', 'ID'),
            'is_valid'      => Yii::t('app_table', '状态'),
            'user_name'     => Yii::t('app_table', '用户名'),
            'wechat_id'     => Yii::t('app_table', '微信关联id'),
            'phone'         => Yii::t('app_table', '手机号'),
            'card_type'     => Yii::t('app_table', '身份类型'),
            'card_number'   => Yii::t('app_table', '身份证号码'),
            'birthday'      => Yii::t('app_table', '生日'),
            'sex'           => Yii::t('app_table', '性别'),
            'mail'          => Yii::t('app_table', '邮箱'),
            'invite_status' => Yii::t('app_table', '是否邀请用户'),
            'invite_id'     => Yii::t('app_table', '邀请用户id'),
            'code'          => Yii::t('app_table', '随机邀请码'),
            'key'           => Yii::t('app_table', '键值，区分产品'),
            'type'          => Yii::t('app_table', '用户类型'),
            'remarks'       => Yii::t('app_table', '备注'),
            'created_at'    => Yii::t('app_table', '创建时间'),
            'updated_at'    => Yii::t('app_table', '修改时间'),
        ];
    }

    /**
     *  保证邀请码唯一
     **/
    public static function randCodeGroup()
    {
        // 查询随机生成的邀请码是否存在
        $randCode = GnhString::randCode();
        $result   = static::find()->where(['code' => $randCode])->all();
        if(empty($result)){
            return $randCode;
        }else{
            static::randCodeGroup();
        }
    }
}
